package cn.cytong.nk0713.services;

import cn.cytong.nk0713.utils.nutz.CustomisedNutDao;
import cn.cytong.rpc.server.NutzRpcService;
import flexjson.JSONSerializer;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.trans.Trans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 访问记录信息
 */
public class VisitRecordService extends NutzRpcService {
    private Logger logger = LoggerFactory.getLogger(VisitRecordService.class);
    private CustomisedNutDao nutDao;

    /**
     * 获取多条访问记录
     * @param params    lastId      开始卡片id
     *                  viewpoint_id    景区Id
     *                  visitAt     开始访问时间
     * @return          visitRecords[visitRecord(card)] 查询访问信息结果
     *                  success true or false
     */
    public String getVisitRecords(Map params) {
        Map result = new HashMap();
        Object lastId = params.get("id");
        Object viewpoint_id = params.get("viewpoint_id");
        String visitAt = (String) params.get("visitAt");
        try {
            List<Record> visitRecords = nutDao.queryTableRecord("CardVisitRecord", "id > ? and viewpoint_id=? and visitAt > ? order by id",
                    lastId, viewpoint_id, visitAt);
            for (Record visitRecord : visitRecords) {
                Record card = nutDao.fetchTableRecord("card", "id = ?", visitRecord.get("card_id"));
                visitRecord.put("card", card);
            }
            result.put("visitRecords", visitRecords);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("error in method ", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return new JSONSerializer().include("visitRecords").serialize(result);
    }

    /**
     * 获取单条访问记录
     * @param params    card_id     卡片Id
     *                  terminal_id 终端Id
     *                  visitAt     访问时间
     * @return          visitRecord 访问记录
     *                  success true or false
     */
    public String getVisitRecord(Map params) {
        Map result = new HashMap();
        Object card_id = params.get("card_id");
        Object terminal_id = params.get("terminal_id");
        String visitAt = (String) params.get("visitAt");
        try {

            result.put("success", true);
        } catch (Exception e) {
            logger.error("error in method ", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return new JSONSerializer().serialize(result);
    }

    /**
     * 更新或添加访问记录
     * @param params    visitRecord     访问记录信息
     * @return          success         true or false
     * @throws Exception
     */
    public String saveVisitRecord(Map params) throws Exception {
        Map result = new HashMap();
        Map visitRecord = (Map) params.get("visitRecord");
        try {
            Trans.begin();
            Chain ch = null;
            Cnd cnd = null;
            for (Object key : visitRecord.keySet()) {
                if (key.equals("id")) {
                    cnd = cnd.where("id", "=", visitRecord.get(key));
                    continue;
                }
                if (ch == null) {
                    ch = Chain.make(key.toString(), visitRecord.get(key));
                } else {
                    ch.add(key.toString(), visitRecord.get(key));
                }
            }
            if (cnd == null) {
                dao.insert("cardVisitRecord", ch);
            } else {
                dao.update("cardVisitRecord", ch, cnd);
            }
            result.put("success", true);
            Trans.commit();
        } catch (Exception e) {
            Trans.rollback();
            logger.error("error in method VisitRecordService,saveVisitRecord", e);
            result.put("success", false);
            result.put("msg", e.getMessage());
        } finally {
            Trans.close();
        }
        return new JSONSerializer().serialize(result);
    }
}
